﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SinsenWeb.Helpers;
using SinsenWeb.Helpers.Filters.ControllerFilters;
using SinsenWeb.Models;
using SinsenWeb.Models.ViewDataTypes;
using System.ComponentModel;

namespace SinsenWeb.Controllers
{
    [AdminAction]
    public class MemberAdminController: BaseController
    {
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            var members = Member.FindAll();
            return View(members);
        }

        public ActionResult New()
        {
            var Member = new Member();
            var Instruments = Instrument.FindAll();
            var Roles = Role.FindAll();
            return View(new EditMember {Member = Member, Instruments = Instruments, Roles = Roles});
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Member Member)
        {
            if(ModelState.IsValid)
            {
                Member.GeneratePasswordHash();
                Member.Instrument = ARGetter<Instrument>.Get(Request.Form["Member.Instrument"]);
                Member.SaveAndFlush();
                MessageHelper.Message("Medlem ble opprettet");
                return RedirectToAction("List");
            }
            else
            {
                MessageHelper.Alert("Noe gikk galt, prøv igjen..");
                return RedirectToAction("New");
            }
       }
 
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            var member = Member.Find(id);
            member.DeleteAndFlush();
            MessageHelper.Message(member.FullName+" ble slettet");
            return RedirectToAction("List");
        }

        public ActionResult Edit(int id)
        {
            var member = Member.Find(id);
            var Instruments = Instrument.FindAll();
            var Roles = Role.FindAll();

            return View(new EditMember { Member = member, Instruments = Instruments, Roles = Roles});
        }

        public ActionResult Update(int id)
        {
            if (ModelState.IsValid)
            {
                var member = Member.Find(id);
                UpdateModel(member, "Member");
                if (!string.IsNullOrEmpty(member.Password))
                {
                    member.GeneratePasswordHash();                    
                }
                var rolesString = Request.Form.GetValues("Member.Roles").Select(val=>int.Parse(val));
                var roles = Role.FindAll(rolesString.ToList());
                member.Roles = roles;
                member.Instrument = Instrument.Get(Request.Form["Member.Instrument"]);
                member.SaveAndFlush();
                MessageHelper.Message("Medlem ble lagret");
                return RedirectToAction("List");
            }
            else
            {
                MessageHelper.Alert("Noe gikk galt, prøv igjen..");
                return RedirectToAction("Edit",id);
            }
        }

    }

    
}
